package com.tools.service.workflow.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tools.mapper.workflow.FlowTransitionMapper;
import com.tools.pojo.workflow.po.FlowTransition;
import com.tools.service.workflow.FlowTransitionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowTransitionServiceImpl extends ServiceImpl<FlowTransitionMapper, FlowTransition> implements FlowTransitionService {



    /**
     * 根据流程定义ID获取所有连线
     */
    @Override
    public List<FlowTransition> getTransitionsByFlowDefinitionId(Long flowDefinitionId) {
        return lambdaQuery()
                .eq(FlowTransition::getFlowDefinitionId, flowDefinitionId)
                .orderByAsc(FlowTransition::getPriority)
                .list();
    }

    /**
     * 根据源节点获取所有出线
     */
    @Override
    public List<FlowTransition> getOutTransitions(Long flowDefinitionId, String sourceNode) {
        return lambdaQuery()
                .eq(FlowTransition::getFlowDefinitionId, flowDefinitionId)
                .eq(FlowTransition::getSourceNode, sourceNode)
                .orderByAsc(FlowTransition::getPriority)
                .list();
    }

    /**
     * 根据目标节点获取所有入线
     */
    @Override
    public List<FlowTransition> getInTransitions(Long flowDefinitionId, String targetNode) {
        return lambdaQuery()
                .eq(FlowTransition::getFlowDefinitionId, flowDefinitionId)
                .eq(FlowTransition::getTargetNode, targetNode)
                .list();
    }

    /**
     * 获取默认连线（无条件连线）
     */
    @Override
    public FlowTransition getDefaultTransition(Long flowDefinitionId, String sourceNode) {
        return lambdaQuery()
                .eq(FlowTransition::getFlowDefinitionId, flowDefinitionId)
                .eq(FlowTransition::getSourceNode, sourceNode)
                .eq(FlowTransition::getTransitionType, "DEFAULT")
                .one();
    }

    /**
     * 获取条件连线
     */
    @Override
    public List<FlowTransition> getConditionTransitions(Long flowDefinitionId, String sourceNode) {
        return lambdaQuery()
                .eq(FlowTransition::getFlowDefinitionId, flowDefinitionId)
                .eq(FlowTransition::getSourceNode, sourceNode)
                .eq(FlowTransition::getTransitionType, "CONDITION")
                .orderByAsc(FlowTransition::getPriority)
                .list();
    }
}

